package com.easyexcell.easyexcellexample.utils.esayexcell.listen;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON;
import com.easyexcell.easyexcellexample.utils.esayexcell.dto.QuoteItemImportDto;
import com.easyexcell.easyexcellexample.utils.esayexcell.vo.QuotationRecordDetailedVo;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author c.c
 * @Since 2023/4/25
 * @Time 16:06
 * ReadListener<T> 中的T为需要导入的类
 * @Description
 */

@Slf4j
public class QuoteImportListener<T> implements ReadListener<QuoteItemImportDto> {

  /**
   * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
   */
  private static final int BATCH_COUNT = 1000;
  /**
   * 缓存的数据
   */
  private final List<QuoteItemImportDto> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
  private List<QuotationRecordDetailedVo> quotationRecordDetailedVos = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);


  @Override
  public void invoke(QuoteItemImportDto quoteItemImportDto, AnalysisContext analysisContext) {
    log.info("解析到一条数据:{}", JSON.toJSONString(quoteItemImportDto));
    cachedDataList.add(quoteItemImportDto);

//    // 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
//    if (cachedDataList.size() >= BATCH_COUNT) {
//      // 存储完成清理 list
//      cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
//    }
  }

  @Override
  public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    quotationRecordDetailedVos = cachedDataList.stream()
      .map(quoteItemImportDto -> {
        QuotationRecordDetailedVo quotationRecordDetailedVo = new QuotationRecordDetailedVo();
        quotationRecordDetailedVo.setName(quoteItemImportDto.getName());
        quotationRecordDetailedVo.setCode(quoteItemImportDto.getCode());
        quotationRecordDetailedVo.setCategoryName(quoteItemImportDto.getCategoryName());
        quotationRecordDetailedVo.setCategoryCode(quoteItemImportDto.getCategoryCode());
        quotationRecordDetailedVo.setSpecification(quoteItemImportDto.getSpecification());
        quotationRecordDetailedVo.setProcurementQuantity(quoteItemImportDto.getProcurementQuantity());
        quotationRecordDetailedVo.setUnit(quoteItemImportDto.getUnit());
        quotationRecordDetailedVo.setRemark(quoteItemImportDto.getRemark());
        quotationRecordDetailedVo.setTaxRate(quoteItemImportDto.getTaxRate());
        quotationRecordDetailedVo.setNoTaxUnitPrice(quoteItemImportDto.getNoTaxUnitPrice());
        quotationRecordDetailedVo.setNoTaxAllPrice(quoteItemImportDto.getNoTaxAllPrice());
        quotationRecordDetailedVo.setTaxUnitPrice(quoteItemImportDto.getTaxUnitPrice());
        quotationRecordDetailedVo.setTaxAllPrice(quoteItemImportDto.getTaxAllPrice());
//        quotationRecordDetailedVo.setPlanItemId(uu);
        return quotationRecordDetailedVo;
      }).collect(Collectors.toList());
    log.info("所有数据解析完成！");
  }

  @Override
  public void onException(Exception exception, AnalysisContext context) throws Exception {
    log.error("解析失败，但是继续解析下一行:{}", exception.getMessage());
    // 如果是某一个单元格的转换异常 能获取到具体行号
    // 如果要获取头的信息 配合invokeHeadMap使用
    if (exception instanceof ExcelDataConvertException) {
      ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;
      log.error("第{}行，第{}列解析异常，数据为:{}", excelDataConvertException.getRowIndex(),
        excelDataConvertException.getColumnIndex(), excelDataConvertException.getCellData());
    }
  }

  public List<QuotationRecordDetailedVo> getQuotationRecordDetailedVos() {
    return quotationRecordDetailedVos;
  }

    public List<QuoteItemImportDto> getCachedDataList() {
        return cachedDataList;
    }
}
